package com.war.service.stock;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.stock.TransferDao;
import com.war.pojo.stock.TransferInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class TransferInfoServiceImpl implements TransferInfoService {
    @Autowired(required = false)
    TransferDao transferDao;


    @Override
    public HashMap<String, Object> index(TransferInfo transferInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(transferInfo.getPage(),transferInfo.getRow());
        String con = transferInfo.getCondition();
        //根据用户选择的查询条件，判断用户需要查询的
        List<TransferInfo> list = new ArrayList<TransferInfo>();
        //判断用户输入的查询条件是否有值
        if(con.equals("")||transferInfo.getConValue().equals("")){
            list  = transferDao.selectAll();
        }else if (con.equals("product_name")){
            list = transferDao.selectByProductName(transferInfo);
        }else if (con.equals("in_warehouse_name")){
            list = transferDao.selectByInWareName(transferInfo);
        }else if (con.equals("out_warehouse_name")){
            list = transferDao.selectByOutWareName(transferInfo);
        }

        //3.把查询的数据转换成分页对象
        PageInfo<TransferInfo> page = new PageInfo<TransferInfo>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());
        return map;
    }

    @Override
    public HashMap<String, Object> add(TransferInfo transferInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = transferDao.add(transferInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> deleteById(TransferInfo transferInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = transferDao.deleteById(transferInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> updateById(TransferInfo transferInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = transferDao.updateById(transferInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> selectAll(TransferInfo transferInfo) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        return map;
    }

    @Override
    public HashMap<String, Object> selectById(TransferInfo transferInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<TransferInfo> list = transferDao.selectById(transferInfo);
        map.put("list",list);
        return map;
    }
}
